package org.example.zpqy.controll;

import org.example.zpqy.bean.AppleGround;
import org.example.zpqy.service.AppleGroundService;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.time.Duration;

@RestController
@RequestMapping("/api/appleground")
public class AppleGroundController {
    private final AppleGroundService service;

    public AppleGroundController(AppleGroundService service) {
        this.service = service;
    }

    // 使用SSE（Server-Sent Events）实现每3秒推送一次数据
    @GetMapping(value = "/stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<AppleGround> streamData() {
        return Flux.interval(Duration.ofSeconds(3))
                .map(tick -> service.getNextData());
    }
}